<?php
/**
* 应用控制器基类
*/
class Core_AdminController extends Core_Controller
{
   public $metaConfig;
    function __construct()
    {
        $session = self::session();
        parent::__construct();
        $this->setMetaConfig();
    }
    /**
     * 初始化本插件的 Meta 配置， 配置来自于 Explugin/插件名称/meta.xml
     *
     * @access private
     */
    function setMetaConfig()
    {
        $className = get_class($this);
        $className = explode('_', $className);
        $className = current($className);
        $file = EXMODULE_DEF_DIR . DS . $className . DS . 'meta.xml';

        if (!file_exists($file)) {
            $file = EXMODULE_CUR_DIR . DS . $className . DS . 'meta.xml';
            if (!file_exists($file)) {
                return;
            }
        }
        // xml file to array format
        $datas = sfdecode_xml_file_to_array($file);

        foreach ($datas as $k => $data) {
            if ( ! isset($data['meta']) || ! is_array($data['meta'])) {
                return;
            } else {
                $metas = $data['meta'];
            }
            foreach ($metas as $key => $meta) {
                if (is_numeric($key)) {
                    $this->metaConfig[$k][$key] = $meta;
                    if (isset($meta['value']['option'])) {
                        if (is_array($meta['value']['option'])) {       // value 下只有一个 option
                            $this->metaConfig[$k][$key]['value'] = $meta['value']['option'];
                        }
                    } else {
                        if ($meta['type'] == 'text' && empty($meta['value'])) {
                            $this->metaConfig[$k][$key]['value'] = array('value');
                        }
                    }
                } else {    // 只有一个 meta
                    $this->metaConfig[$k][0] = $metas;
                    if (isset($metas['value']['option'])) {
                        if (is_array($metas['value']['option'])) {
                            $this->metaConfig[$k][0]['value'] = $metas['value']['option'];
                        }
                    } else {
                        if ($metas['type'] == 'text' && empty($metas['value'])) {
                            $this->metaConfig[$k][0]['value'] = array('value');
                        }
                    }
                    break;
                }
            }
        }
    }
    /**
     * 获取配置
     * @access private
     */
    function getMetaConfig($key)
    {
        if (isset($this->metaConfig[$key])) {
            return $this->metaConfig[$key];
        } else {
            return array();
        }
    }

    public function sign($admin)
    {
        if ($admin instanceof IAdmin) {
            $key = sprintf('__%s', $admin->getType());
            $data = array(
                'id' => $admin->getId()
                , 'name' => $admin->getName()
                , 'email' => $admin->getEmail()
                , 'telephone' => $admin->getTelephone()
                , 'type' => $admin->getType()
                , 'grade' => $admin->getGrade()
                , 'config' => $admin->getConfig()
            );
            if (empty($data['id'])) {
                return $this->setErrorCode(1001)->setError('参数错误：id');
            }
            if (empty($data['name'])) {
                return $this->setErrorCode(1001)->setError('参数错误：name');
            }
            if (empty($data['type'])) {
                return $this->setErrorCode(1001)->setError('参数错误：type');
            }
            $eventResult = sftrigger_event('admin_sign', 1, $data);
            if (!sfarray_true($eventResult)) {
                $error = sfget_event_exception('admin_sign', new Exception('不允许登录'));
                return $this->setException($error);
            }
            $_SESSION[$key] = $data;
            return $_SESSION[$key];
        }
        return null;
    }

    public function signout($admin)
    {
        if (is_string($admin)) {
            $key = sprintf('__%s', $admin);
        } elseif ($admin instanceof IAdmin) {
            $key = sprintf('__%s', $admin->getType());
        }
        unset($_SESSION[$key]);
        session_write_close();
        return true;
    }

    public function valid($type = 'default', $loginReturn = false, $login = '/admin/login')
    {
        $sessionID = session_id();
        if (empty($sessionID)) {
            session_start();
        }
        $key = sprintf('__%s', $type);
        if (empty($_SESSION[$key])) {
            if ($loginReturn) {
                return null;
            } else {
                sfredirect($login);
            }
        }
        return $_SESSION[$key];
    }

    static function isSuper()
    {
        $session = self::session();
        if (isset($session['admin']['admin__privilege_id']) && empty($session['admin']['admin__privilege_id']))
        {
            return true;
        }
        return false;
    }

    static function isCurAdmin($data)
    {
        $session = self::session();
        return $session['admin']['admin__id'] == $data['id'];
    }
}